package com.example.wificov;


import java.util.List;
import com.example.database.Point;
import com.example.database.myDBHelper;
import com.example.wificovv2.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ShowPowpicture extends Activity { 
	
	myDBHelper db;
	
	
	LinearLayout mLinearLayout;
	
	Paint paint1 = new Paint();
	
	Paint paint2 = new Paint();
	
	Paint paint3 = new Paint();
	
	int height = 0, width = 0, imgheight = 0, imgwidth=0;
	
	float ratio1 = 0f, ratio2 = 0f;
	
	Resources res;

	Bitmap bm;
	
	BitmapDrawable bd;
	ImageView picture;
	ListView lvssid;
	EditText filepowname;
	
	//Cac thu vien muc mau can thiet
	int level1, level2, level3, level4, level5, level6, level7, level8, level9;
	int level10, level11, level12, level13, level14, level15;
	//
	Button load, save, finish;
	Canvas c;
	String value = "";
	int APID,  pos;
	String SSID;
	List<Point> list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12, list13, list14, list15;
	float[] data1,data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12, data13, data14, data15;

	String nameAP;
	int idAP;
	
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_powpicture);
		db = new myDBHelper(this);
		
		    mLinearLayout = new LinearLayout(this);
		    picture = (ImageView) findViewById(R.id.picpow);
		    load = (Button) findViewById(R.id.loadpow);
		    save = (Button) findViewById(R.id.powsave);
		    filepowname = (EditText) findViewById(R.id.filepowname);
		    finish = (Button) findViewById(R.id.finishdispow);
		    lvssid = (ListView) findViewById(R.id.ssidpow);
		   
			//1. Khởi tạo dữ liệu cho mảng arr 
		    List<String> name = db.getAPName(APID);
			final String []  arr = new String[name.size()]; 

		  	ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name.toArray(arr));
		  	//4. Đưa Data source vào ListView
		  	lvssid.setAdapter(adapter);

	    lvssid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				 pos =  position;
				 // Lấy tên của AP trong vị trí được chọn
				 String item = parent.getAdapter().getItem(position).toString();
				 String[] splitItem = item.split(" ");
				 idAP = Integer.parseInt(splitItem[0]);
				 nameAP = splitItem[1];
				 db.n(idAP); 				
				 db.RSSILOS(idAP);
				 int k = db.numObs(idAP);
				 for (int i=1; i<=k; i++){
					 db.RSSINLOS(idAP, i);
				 }
				 db.Level(idAP);
					
					//Initilize ImageView to contain picture
					level1 = getResources().getColor(R.color.level1);
					level2= getResources().getColor(R.color.level2);
					level3= getResources().getColor(R.color.level3);
					level4= getResources().getColor(R.color.level4);
					level5= getResources().getColor(R.color.level5);
					level6= getResources().getColor(R.color.level6);
					level7= getResources().getColor(R.color.level7);
					level8= getResources().getColor(R.color.level8);
					level9= getResources().getColor(R.color.level9);
					level10= getResources().getColor(R.color.level10);
					level11= getResources().getColor(R.color.level11);
					level12= getResources().getColor(R.color.level12);
					level13= getResources().getColor(R.color.level13);
					level14= getResources().getColor(R.color.level14);
					level15= getResources().getColor(R.color.level15);					
				
						 data1 = db.getPoint2(pos, 1);
						 data2 = db.getPoint2(idAP, 2);						 
						 data3 = db.getPoint2(idAP, 3);						
						 data4 = db.getPoint2(idAP, 4);						
						 data5 = db.getPoint2(idAP, 5);						 
						 data6 = db.getPoint2(idAP, 6);						
						 data7 = db.getPoint2(idAP, 7);						
						 data8 = db.getPoint2(idAP, 8);						
						 data9 = db.getPoint2(idAP, 9);						
						 data10 = db.getPoint2(idAP, 10);						
						 data11 = db.getPoint2(idAP, 11);						
						 data12 = db.getPoint2(idAP, 12);						
						 data13 = db.getPoint2(idAP, 13);						 
						 data14 = db.getPoint2(idAP, 14);						
						 data15 = db.getPoint2(idAP, 15);
					
			 }

			private String getText(String string) {
				// TODO Auto-generated method stub
				return null;
			}
			 });
	    
	    finish.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ShowPowpicture.this, Wlanpicture.class);
				startActivity(intent);
			}
		});
	    
	    
	    load.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Xét listview được chọn là ssid nào 
				//Lấy dữ liệu từ ssid đó đưa vào mảng data1 hoặc 2
				APID = pos;	
				
				res = getResources();
				    //Get image's size
				    BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
				    dimensions.inJustDecodeBounds = true;
				    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.newmap, dimensions);
				    imgheight = dimensions.outHeight;
				    imgwidth =  dimensions.outWidth;
				    
				    //calculate the ratios between image's size and real world distance - how many pixels per meter
				    String[] size = db.getMapsize(2);
					ratio1 = (float)imgwidth/(Integer.parseInt(size[0]));
				    ratio2 = (float)imgheight/(Integer.parseInt(size[1]));
//				    ratio1 = (float)imgwidth/160;
//				    ratio2 = (float)imgheight/100;
				    
				    //cast from immutableBitmap to mutableBitmap to insert into Canvas 
				    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.newmap );
				    Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
				    Canvas c = new Canvas( mutableBitmap );
				   
				    //set attributes for Paints (used to draw)
				    paint1.setColor( Color.BLUE );	    
				    paint2.setColor( Color.RED );
				    paint2.setStrokeWidth( 2f );   
				    paint3.setColor( Color.GREEN );
				    paint3.setStrokeWidth( 2f );
				    
				    // Get point[] from database like form below
				    //draw line from point to point
//				    float [] data1 = {80 * ratio1, 55 * ratio2, 75 * ratio1, 45 * ratio2,
//				    		75 * ratio1, 45 * ratio2, 65 * ratio1, 40 * ratio2,
//				    		65 * ratio1, 40 * ratio2, 55 * ratio1, 45 * ratio2,
//				    		55 * ratio1, 45 * ratio2, 50 * ratio1, 55 * ratio2,
//				    		50 * ratio1, 55 * ratio2, 50 * ratio1, 60 * ratio2, 
//				    		50 * ratio1, 60 * ratio2, 65 * ratio1, 55 * ratio2,
//				    		65 * ratio1, 55 * ratio2, 75 * ratio1, 60 * ratio2,
//				    		75 * ratio1, 60 * ratio2, 80 * ratio1, 55 * ratio2};
//				    float[] data2 = {90 * ratio1, 55 * ratio2, 85 * ratio1, 40 * ratio2, 
//				    		85 * ratio1, 40 * ratio2, 75 * ratio1, 35 * ratio2,75 * ratio1, 35 * ratio2, 
//				    		65 * ratio1, 30 * ratio2,65 * ratio1, 30 * ratio2, 60 * ratio1, 35 * ratio2,
//				    		60 * ratio1, 35 * ratio2, 45 * ratio1, 35 * ratio2, 
//				    		45 * ratio1, 35 * ratio2, 40 * ratio1, 55 * ratio2,
//				    		40 * ratio1, 55 * ratio2, 40 * ratio1, 55 * ratio2,
//				    		40 * ratio1, 55 * ratio2, 45 * ratio1, 70 * ratio2,
//				    		45 * ratio1, 70 * ratio2, 65 * ratio1, 75 * ratio2,
//				    		65 * ratio1, 75 * ratio2, 70 * ratio1, 75 * ratio2,
//				    		70 * ratio1, 75 * ratio2, 75 * ratio1, 70 * ratio2,
//				    		75 * ratio1, 70 * ratio2, 85 * ratio1, 65 * ratio2,
//				    		85 * ratio1, 65 * ratio2, 90 * ratio1, 55 * ratio2,};
			//	    c.drawLines(data1, paint2);
			//	    c.drawLines(data2, paint3);
				    drawPoly(c, level1, data1);
				    drawPoly(c, level2, data2);
				    drawPoly(c, level3, data3);
				    drawPoly(c, level4, data4);
				    drawPoly(c, level5, data5);
				    drawPoly(c, level6, data6);
				    drawPoly(c, level7, data7);
				    drawPoly(c, level8, data8);
				    drawPoly(c, level9, data9);
				    drawPoly(c, level10, data10);
				    drawPoly(c, level11, data11);
				    drawPoly(c, level12, data12);
				    drawPoly(c, level13, data13);
				    drawPoly(c, level14, data14);
				    drawPoly(c, level15, data15);
				    //set ImageBitmap to ImageView
				    picture.setImageBitmap( mutableBitmap );
				    //
			}});
	     
	    save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// Luu ImageView thành file ảnh
				
			}
		});
	}
	//ham ve cho mot muc mau
	private void drawPoly(Canvas canvas, int color, float[] points) {
	    // line at minimum...
	    if (points.length < 2) {
	        return;
	    }
	    // paint
	    else{
		    Paint polyPaint = new Paint();
		    polyPaint.setColor(color);
		    polyPaint.setStyle(Paint.Style.FILL);
		    polyPaint.setStrokeWidth(5f);
		    polyPaint.setAlpha(0x40);
		    // path
		    Path polyPath = new Path();
		    polyPath.moveTo(points[0], points[1]);
		    int i, len;
		    len = points.length;
		    for (i = 2; i < len; i+=2) {
		        polyPath.lineTo(points[i], points[i+1]);
		    }
		    polyPath.lineTo(points[0], points[1]);
	
		    // draw
		    canvas.drawPath(polyPath, polyPaint);
	    }
	}
	
//	public float[] getFloats(List<Float> values) {
//	    int length = values.size();
//	    float[] result = new float[length];
//	    for (int i = 0; i < length; i++) {
//	      result[i] = values.get(i).floatValue();
//	    }
//	    return result;
//	  }

//	imageView.setOnTouchListener(new OnTouchListener() {
//        @Override
//        public boolean onTouch(View v, MotionEvent event) {
//            ImageView view = (ImageView) v;
//
//            switch (event.getAction() & MotionEvent.ACTION_MASK) {
//            case MotionEvent.ACTION_DOWN:
//                savedMatrix.set(matrix1);
//                start.set(event.getX(), event.getY());
//                mode = DRAG;
//                break;
//            case MotionEvent.ACTION_POINTER_DOWN:
//                oldDist = spacing(event);
//                if (oldDist > 10f) {
//                    start.set(event.getX(), event.getY());
//                    savedMatrix.set(matrix1);
//                    midPoint(mid, event);
//                    // mode = POINT2;
//                    mode = ZOOM;
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                mode = NONE;
//                distanceOffset = minOffset;
//            case MotionEvent.ACTION_POINTER_UP:
//                mode = NONE;
//                distanceOffset = minOffset;
//                break;
//            case MotionEvent.ACTION_MOVE:
//                if (mode == POINT2) {
//                    newDist = spacing(event);
//                    if (newDist - oldDist > 5f
//                            || newDist - oldDist < -5f) {
//                        mode = ZOOM;
//                    } else {
//                        start.set(event.getX(), event.getY());
//                        mode = DRAG;
//                    }
//                } else if (mode == DRAG) {
//                    matrix1.set(savedMatrix);
//                    matrix1.postTranslate(event.getX() - start.x,
//                            event.getY() - start.y);
//                } else if (mode == ZOOM) {
//                    newDist = spacing(event);
//                    if (newDist > 10f) {
//                        matrix1.set(savedMatrix);
//                        float scale = newDist / oldDist;
//                        matrix1.postScale(scale, scale, mid.x,
//                                mid.y);
//                        finalscale = scale;
//                    }
//                }
//                break;
//            }
//
//            view.setImageMatrix(matrix1);
////              matrixTurning(matrix1, view);
//            return true; // indicate event was handled
//        }
//    });
//}
//private void limitZoom(Matrix m) {
//
//    float[] values = new float[9];
//    m.getValues(values);
//    float scaleX = values[Matrix.MSCALE_X];
//    float scaleY = values[Matrix.MSCALE_Y];
//    if(scaleX > MAX_ZOOM) {
//        scaleX = MAX_ZOOM;
//    } else if(scaleX < MIN_ZOOM) {
//        scaleX = MIN_ZOOM;
//    }
//
//    if(scaleY > MAX_ZOOM) {
//        scaleY = MAX_ZOOM;
//    } else if(scaleY < MIN_ZOOM) {
//        scaleY = MIN_ZOOM;
//    }
//
//    values[Matrix.MSCALE_X] = scaleX;
//    values[Matrix.MSCALE_Y] = scaleY; 
//    m.setValues(values);
//}
}

	    
